<?php
/**
 * 命令行执行 web模式无法执行
 * Created by lni_wang.
 * Product: PhpStorm
 * Date: 2017/4/6 18:49
 * File: Cli.php
 */
ini_set('default_socket_timeout', -1); #防止socket超时
class Cli extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        if(!is_cli()){
            show_404();
        }
    }

    function test()
    {
        $this->load->model('Task_model');
        $this->Task_model->action_task([
            'link'        => 'http://bbs.5iyq.com/forum.php',
            'content_md5' => '',
        ]);
//        include APPPATH.'third_party/Snoopy-master/Snoopy.class.php';
//        $Snoopy = new Snoopy();
//        $Snoopy->fetch('http://www.21yq.com/');
//        var_dump($Snoopy->response_code);
//        var_dump(strpos($Snoopy->response_code, '200'));
//        var_dump(strpos($Snoopy->response_code, '200') === false);
//
//        if(strpos($Snoopy->response_code, '200') !== false){
//            var_dump($Snoopy->results);
//            return $Snoopy->results;
//        }else{
//            return false;
//        }

    }

    /**
     * 队列接受端
     */
    function subscribe()
    {
        $this->load->library('Redis_mq', null, 'Mq');
        $this->Mq->subscribe('real', function($instance, $channelName, $message){
            $this->subscribe_callback($message);
        });
    }

    /**
     * 检查用户连接, 最后提交时间, 超过2小时, 重新加入队列
     */
    function link_to_subscribe()
    {
        $this->load->model('Link_cache_model');

        $links = $this->Link_cache_model->get_host_link([
            'start_time' => time()-86400,
            'end_time' => time()-7200,
        ]);
        if(count($links)){
            $this->load->model('Task_model');
            foreach($links as $link){
                $this->Task_model->add_task([
                    'link' => $link
                ]);

                $this->Link_cache_model->add_host_link([
                    'link'      => $link,
                    'last_time' => time(),
                ]);
            }
        }
    }


    function subscribe_time()
    {
        $this->load->library('Redis_mq', null, 'Mq');
        $this->Mq->subscribe('time_task', function($instance, $channelName, $message){
            $this->subscribe_callback($message);
        });
    }

    private function subscribe_callback($message)
    {
        try{
            $data = json_decode($message, true);
            $model      = $data['model'];
            $function   = $data['function'];

            $this->load->model($model);
            if(isset($data['params'])){
                $this->$model->$function($data['params']);
            }else{
                $this->$model->$function();
            }
        }catch (SysException $e){
            print_r($e->getMessage());
        }catch(Exception $e){
            print_r($e->getMessage());
        }
    }

    function time_task()
    {
        $this->load->library('Redis_mq', null, 'Mq');
        while(true){
            $this->Mq->time_to_real();
            sleep(5);
        }
    }
}